﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using ScrumTable.Common;

#endregion

namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation
{
    /// <summary>
    /// Specifies a member handler which is used to search for the
    /// TFS project members.
    /// </summary>
    public class TfsMemberLoader
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IIdentityManagementService _memberManagerSvc;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TfsMemberLoader instance, no public constructor
        /// needed.
        /// </summary>
        public TfsMemberLoader(IIdentityManagementService memberSvc)
        {
            PreCondition.AssertNotNull(memberSvc, "memberSvc");

            _memberManagerSvc = memberSvc;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a list of all members to a TfsProject. Includes the members from the default collection.
        /// </summary>
        /// <returns></returns>
        public ICollection<TeamFoundationIdentity> GetMembers(ICollection<string> displayNames)
        {
            PreCondition.AssertNotNull(displayNames, "displayNames");

            TeamFoundationIdentity[][] rootIdentities = _memberManagerSvc.ReadIdentities(
                IdentitySearchFactor.DisplayName,
                displayNames.ToArray(),
                MembershipQuery.None,
                ReadIdentityOptions.None);
            ICollection<TeamFoundationIdentity> membersIdentity = new List<TeamFoundationIdentity>();

            foreach (TeamFoundationIdentity[] rootIdentity in rootIdentities)
            {
                foreach (TeamFoundationIdentity memberIdentity in rootIdentity)
                {
                    membersIdentity.Add(memberIdentity);
                }
            }
            return membersIdentity;
        }


        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}